﻿using taurus.client;
using UnityEngine;

public class Demo : MonoBehaviour
{
    
    void Awake()
    {
        taurus.client.Logger.loggerPrint = (lev, msg) =>
        {
            switch (lev)
            {
                case LoggerLevel.INFO:
                case LoggerLevel.DEBUG:
                case LoggerLevel.TRACE:
                    UnityEngine.Debug.Log(msg);
                    break;
                case LoggerLevel.WARN:
                    UnityEngine.Debug.LogWarning(msg);
                    break;
                case LoggerLevel.ERROR:
                    UnityEngine.Debug.LogError(msg);
                    break;
            }
        };
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        Application.targetFrameRate = 30;
        Application.runInBackground = true;
        Input.multiTouchEnabled = false;
    }


    // Use this for initialization
    void Start()
    {
    }

    public void OnHttpTest()
    {
        TaurusClient client = new TaurusClient("http://127.0.0.1/", "test", ConnectionProtocol.Web);
        client.connect();
        client.send("test", TObject.newInstance(), (res) =>
        {
            Debug.Log(res.returnCode);
        });
    }

    public void OnTcpTest()
    {
        ITObject validate = TObject.newInstance();
        validate.putString("user", "tt001");
        TaurusClient client = new TaurusClient("127.0.0.1:10010", "test", ConnectionProtocol.Tcp);
        client.connect(validate);
        client.addEventListener(NetClientEvent.Connect, (evt) =>
        {
            SocketCode code = (SocketCode)evt.getParameter("code");

            if (code == SocketCode.Connect)
            {
                ITObject _data = new TObject();
                _data.putInt("pram", 111);
                client.send("t1/tcp_test", _data, null);
            }
            else
            {
                Debug.Log("连接失败");
                client.killConnection();
            }
        });
        client.addEventListener(NetClientEvent.OnEvent, (evt) =>
        {
            var msg = evt.getParameter("msg") as Message;
            Debug.Log(msg.useUdp ? "udp" : "tcp" + " -> " + msg.command);
        });
    }

    void Update()
    {
        NetManager.processEvents();
    }




    void OnApplicationQuit()
    {
        NetManager.killAllConnection();
    }




}